package cn.demoncat.util.web.constant;

import cn.demoncat.util.io.constant.ImgConstant;
import cn.demoncat.util.io.constant.OfficeConstant;
import cn.demoncat.util.lang.StringUtil;
import cn.demoncat.util.lang.entity.MapSs;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * 常用MIME类型（MediaType、ContentType）
 * 
 * 对照表：https://tool.oschina.net/commons
 * 
 * @author 延晓磊
 *
 * @since 2020年6月4日
 */
public class MimeConstant {

	// ================================= 文本 ================================= //
	
	/**
	 * JSON
	 */
	public static final String JSON = "application/json";

	/**
	 * JSON-UTF8
	 */
	public static final String JSON_UTF8 = "application/json;charset=UTF-8";
	
	// ================================= 文件 ================================= //
	
	/**
	 * 通用的二进制文件下载类型
	 */
	public static final String DOWNLOAD = "application/octet-stream";
	
	/**
	 * PDF：pdf
	 */
	public static final String PDF = "application/pdf;charset=UTF-8";
	
	/**
	 * Word：doc
	 */
	public static final String DOC = "application/msword;charset=UTF-8";
	
	/**
	 * Word：docx
	 */
	public static final String DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=UTF-8";
	
	/**
	 * Excel：xls
	 */
	public static final String XLS = "application/vnd.ms-excel;charset=UTF-8";
	
	/**
	 * Excel：xlsx
	 */
	public static final String XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8";
	
	/**
	 * Image：png
	 */
	public static final String PNG = "image/png";
	/**
	 * Image：jpg
	 */
	public static final String JPG = "image/jpeg";
	/**
	 * Image：jpeg
	 */
	public static final String JPEG = "image/jpeg";
	/**
	 * Image：gif
	 */
	public static final String GIF = "image/gif";
	/**
	 * Image：bmp
	 */
	public static final String BMP = "image/bmp";
	/**
	 * Image：wbmp
	 */
	public static final String WBMP = "image/vnd.wap.wbmp";
	
	/**
	 * MIME类型映射：文件扩展名 - MimeType
	 * 
	 * @author 延晓磊
	 *
	 * @since 2020年6月4日
	 */
	public static final Map<String, String> MIME_MAP = MapSs.getInstance(
				OfficeConstant.Format.DOC, DOC,
				OfficeConstant.Format.DOCX, DOCX,
				OfficeConstant.Format.XLS, XLS,
				OfficeConstant.Format.XLSX, XLSX,
				OfficeConstant.Format.PDF, PDF,
				
				ImgConstant.Format.PNG, PNG,
				ImgConstant.Format.JPEG, JPEG,
				ImgConstant.Format.JPG, JPG,
				ImgConstant.Format.GIF, GIF,
				ImgConstant.Format.BMP, BMP,
				ImgConstant.Format.WBMP, WBMP
			);
	
	/**
	 * 获取MIME类型
	 * 
	 * @param extension	文件扩展名
	 * 
	 * @return MimeType
	 * 
	 * @author 延晓磊
	 *
	 * @since 2020年6月4日
	 */
	public static String get(String extension) {
		if (StringUtils.isBlank(extension)) {
			return DOWNLOAD;
		}
		// 通过映射获取，默认二进制下载
		return StringUtil.toDefault(MIME_MAP.get(extension), DOWNLOAD);
	}
}
